$("#document").ready(function() {
	var idStr = getQueryString("id");
	console.log(idStr);
	// 报告的id
	var id = idStr.split("_")[0];
	console.log(id);
	getDaiypublishDetail(id);

	// 全选事件
	$("#check_all").click(function() {
		// 获取checkbox对象组
		var subbox = $("input[name='viewbox']")

		if ($("#check_all").is(":checked")) {
			$.each(subbox, function(index, value) {
				this.checked = true;
			});
		} else {
			$.each(subbox, function(index, value) {
				this.checked = false;
			});
		}

	})

	// 反选事件
	$("#check_opst").click(function() {
		$("input[name='viewbox']").each(function() {
			if (this.checked == false) {
				this.checked = true;
			} else {
				this.checked = false;
			}
		});
	})

	// 确认发布按钮
	$("#publishview").click(function() {
		var checkarray = "";
		$("input[name='viewbox']").each(function() {
			if (this.checked == true) {
				var id = this.id;
				checkarray = checkarray + id + "_";
			}
		})
		console.log("checkarray=" + checkarray);
		saveChooseOpinion(checkarray, id);
	});

})
// 获取当前url参数
function getQueryString(paraname) {
	var reg = new RegExp("(^|&)" + paraname + "=([^&]*)(&|$)", "i");
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return (r[2]);
	}
	return null;
}

// 获取每日报告详情
function getDaiypublishDetail(id) {
	var url = "../DailySentimentServlet?method=getReportById";
	$.ajax({
		type : "get",
		url : url,
		data : {
			"id" : id
		},
		success : function(data) {
			var report = JSON.parse(data).report;
			generateDetailView(report);

		},
		error : function() {
			console.log("error");
		}
	})
}
function generateDetailView(report) {
	var focustitle = report.title;
	$("#title").text(focustitle);

	var content = report.text;
	var htmlContent = "";
	for ( var i in content) {
		content[i] = content[i].split(" 　　");
		for ( var j in content[i]) {
			if (content[i][j].charAt(0) == "　"
					&& content[i][j].charAt(1) == "　")
				htmlContent += "<p>" + content[i][j] + "</p>";
			else
				htmlContent += "<p>　　" + content[i][j] + "</p>";
			content[i][j] = "<p>" + content[i][j] + "</p>";
		}
	}
	$("#maincontent").html(htmlContent);
	var time = report.date;
	$("#time").append(time);

	if (report.opinion == null) {
		var opinions = report.opinion;
		console.log(opinions);
		$
				.each(
						opinions,
						function(index, value) {

							var otherview = "<label class='checkbox-inline'><input type='checkbox' id='"
									+ this._id
									+ "' name='viewbox'  value='"
									+ this.publisher
									+ "'> <p style='font-weight: 550;line-height:25px' class='text-justify'><strong>"
									+ this.publisher
									+ "：</strong>"
									+ this.text
									+ "</p></label>"
							$("#otherview").append(otherview);
						})
	}

}

function saveChooseOpinion(checkarray, id) {
	// 这里的url应为处理opinion的servlet 需修改
	var url = "../DailySentimentServlet?method=deleteOpinions"
	$.ajax({
		type : "get",
		url : url,
		data : {
			"checkarray" : checkarray,
			"id" : id
		},
		success : function(data) {
			console.log(data);
		},
		error : function() {
			console.log("error");
		}

	})
}
